package com.ipharma.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import oracle.jdeveloper.layout.GridBagConstraints2;

import com.ipharma.McdError;
import com.ipharma.McdPrs;
import com.ipharma.config.Config;
import com.ipharma.dto.Task;

/**
 * A Swing-based panel class.
 * <P>
 * @author JorgE.H
 */
public class GUITask extends JPanel
{
  /**
   * Panel
   */
  GridBagLayout layoutBase = new GridBagLayout();
  JLabel jLblDesc = new JLabel();
  JLabel jLblOrigin = new JLabel();
  GUIButtonImage jButtQty;
  GUIButtonImage jButtBatch;
  GUIButtonImage jButtOk;
  public static Dimension descCell = new Dimension( 220, 30 );

  /**
   * Attributes
   */
  Task task;
  GUIOperation myContainer;

  /**
   * Constructs a new instance.
   */
  public GUITask( GUIOperation pContainer, Task pTask )
  {
    super();
    try
    {
      myContainer = pContainer;
      task = pTask;
      jbInit();
    }
    catch( Exception e )
    {
      McdError.add( e );
    }
  }

  /**
   * Initializes the state of this instance.
   */
  private void jbInit() throws Exception
  {
    this.setLayout( layoutBase );
    this.setBackground( Colors.RCE_BACK );
    this.setBorder( BorderFactory.createLineBorder( Colors.APN_BORDER, 1 ) );

    // description label
    jLblDesc.setText( task.getDescription() );
    jLblDesc.setFont( Texts.fntTask );
    jLblDesc.setForeground( Colors.RCE_TEXT );
    jLblDesc.setHorizontalAlignment( SwingConstants.CENTER );
    jLblDesc.setMaximumSize( descCell );
    jLblDesc.setPreferredSize( descCell );

    // batch number button
    jButtBatch = new GUIButtonImage( "" + task.getBatchNumber(), Images.buttBatch, Images.buttBatchDn );
    jButtBatch.setDisabledIcon( Images.buttBatchDn );
    jButtBatch.setFont( Texts.fntTaskButt );
    jButtBatch.setForeground( Colors.RCE_TITLE_TEXT );
    jButtBatch.addActionListener( new GUITask_jButtBatch_actionAdapter( this ) );
    jButtBatch.setEnabled( false );

    // origin label
    jLblOrigin.setText( task.getOrigin() );
    jLblOrigin.setIcon( Images.buttSource );
    jLblOrigin.setFont( Texts.fntTask );
    jLblOrigin.setForeground( Colors.RCE_TITLE_TEXT );
    jLblOrigin.setHorizontalTextPosition( SwingConstants.CENTER );
    jLblOrigin.setVerticalTextPosition( SwingConstants.CENTER );

    // quantity text field
    jButtQty = new GUIButtonImage( "" + task.getQuantity(), Images.buttQty, Images.buttQtyDn );
    jButtQty.setDisabledIcon( Images.buttQtyDn );
    jButtQty.setFont( Texts.fntTask );
    jButtQty.setForeground( Colors.RCE_TITLE_TEXT );
    jButtQty.addActionListener( new GUITask_jButtQty_actionAdapter( this ) );
    jButtQty.setEnabled( false );

    // button OK
    jButtOk = new GUIButtonImage( Images.buttDone, Images.buttDoneDn );
    jButtOk.addActionListener( new GUITask_jButtOk_actionAdapter( this ) );

    // adds objects to panel
    this.add( jLblDesc, new GridBagConstraints2( 0, 0, 1, 1, 0.0, 1.0,
        GridBagConstraints.WEST, GridBagConstraints.BOTH, Texts.zeroMargin, 0, 20 ) );
    this.add( jButtBatch, new GridBagConstraints2( 1, 0, 1, 1, 0.0, 0.0,
        GridBagConstraints.EAST, GridBagConstraints.NONE, Texts.zeroMargin, 0, 0 ) );
    this.add( jLblOrigin, new GridBagConstraints2( 2, 0, 1, 1, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.NONE, Texts.zeroMargin, 0, 0 ) );
    this.add( jButtQty, new GridBagConstraints2( 3, 0, 1, 1, 0.0, 0.0,
        GridBagConstraints.WEST, GridBagConstraints.NONE, Texts.zeroMargin, 0, 0 ) );
    this.add( jButtOk, new GridBagConstraints2( 4, 0, 1, 1, 1.0, 0.0,
        GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 15, 0, 0 ), 0, 0 ) );
  }

  /**
   * Sets quantity field text editable on or off
   * @param pState boolean new state
   */
  public void setEditableQuantity( boolean pState )
  {
    this.jButtQty.setEnabled( pState );
  }

  /**
   * Sets batch field text editable on or off
   * @param pState boolean new state
   */
  public void setEditableBatch( boolean pState )
  {
    this.jButtBatch.setEnabled( pState );
  }

  void jButtOk_actionPerformed( ActionEvent e )
  {
    myContainer.myContainer.kickTimer();
    if ( task.isDone() )
    {
      task.setDone( false );
      this.jButtOk.setIcon( Images.buttDone );
    }
    else
    {
      task.setDone( true );
      this.jButtOk.setIcon( Images.buttDoneUp );
    }
  }

  void jButtBatch_actionPerformed( ActionEvent e )
  {
    // changes batch number
    myContainer.myContainer.stopTimer();

    int li_newBatch = task.getBatchNumber();
    // displays batch number selection dialog
    GUIBatcher l_qtyDialog = new GUIBatcher( myContainer, "", true );
    l_qtyDialog.setTask( task );
    Config config = (Config) myContainer.theSess.get(McdPrs.CONFIG);
    l_qtyDialog.start(new Long(config.getTimeout()).longValue());
    if ( l_qtyDialog.hasExpired() ) // time expired
    {
      myContainer.myContainer.timeout();
      return;
    }
    // changes bathc number if it changed
    if ( !l_qtyDialog.getSelection().equals( "" ) )
      li_newBatch = Integer.parseInt( l_qtyDialog.getSelection() );

    task.setBatch( li_newBatch );
    // sets displayed batch number
    this.jButtBatch.setText( "" + task.getBatchNumber() );
    // resets timer
    myContainer.myContainer.startTimer();
  }

  void jButtQty_actionPerformed( ActionEvent e )
  {
    // changes quantity
    myContainer.myContainer.stopTimer();

    int li_newQty = task.getQuantity();
    // displays quantity selection dialog
    GUIQuantifierRce l_qtyDialog = new GUIQuantifierRce( myContainer, "", true );
    l_qtyDialog.setTask( task );
    Config config = (Config) myContainer.theSess.get(McdPrs.CONFIG);
    l_qtyDialog.start(new Long(config.getTimeout()).longValue());
    if ( l_qtyDialog.hasExpired() ) // time expired
    {
      myContainer.myContainer.timeout();
      return;
    }
    // li_newQty keeps selected quantity
    li_newQty = Integer.parseInt( l_qtyDialog.getSelection() );

    task.setQuantity( li_newQty );
    // sets displayed quantity
    this.jButtQty.setText( "" + task.getQuantity() );
    // resets timer
    myContainer.myContainer.startTimer();
  }
}

class GUITask_jButtOk_actionAdapter implements java.awt.event.ActionListener
{
  GUITask adaptee;

  GUITask_jButtOk_actionAdapter( GUITask adaptee )
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed( ActionEvent e )
  {
    adaptee.jButtOk_actionPerformed( e );
  }
}

class GUITask_jButtBatch_actionAdapter implements java.awt.event.ActionListener
{
  GUITask adaptee;

  GUITask_jButtBatch_actionAdapter( GUITask adaptee )
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed( ActionEvent e )
  {
    adaptee.jButtBatch_actionPerformed( e );
  }
}

class GUITask_jButtQty_actionAdapter implements java.awt.event.ActionListener
{
  GUITask adaptee;

  GUITask_jButtQty_actionAdapter( GUITask adaptee )
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed( ActionEvent e )
  {
    adaptee.jButtQty_actionPerformed( e );
  }
}
